/**
\author Dr. Zachary Wartell
\file main.cpp

\brief This is simplistic C++ example designed to exercise the Visual Leaker Detector
memory leak ability.  By default the Visual Leaker Detector outputs the results of any
memory leaks when the program terminates in the Visual Studio's IDE's 'Output' tabbed
window.
*/
#include <Car.h>
#include <iostream>
#include <vld.h>

static bool leakMemory; // enable/disable deliberate memory leaks

// silly function to illustrate VLD ability to capture call stack at time of
// leaked memory allocations
void functionB(void)
    {
    Car *car1, *car2;  // Car objects on heap
    Car car3, car4;    // local Car objects on stack
    car3.weight = 10;
    car4.weight = 15;

    car1 = new Car;
    car2 = new Car;
    delete car1;
    if (!leakMemory)
	delete car2;
    }

// silly function to illustrate VLD ability to capture call stack at time of
// leaked memory allocations
void functionA(void)
    {
    Car *car1, *car2; // Car objects on heap
    Car car3, car4;   // local Car objects on stack
    car3.weight = 10;
    car4.weight = 15;
    Car *car5;
    Car car6;
    car6.weight = 999;

    car1 = new Car;
    car2 = new Car;
    car5 = new Car;
    delete car1;
    functionB();
    if (!leakMemory)
	delete car2;
	delete car5;
    }

int main (int argc, char** argv)
    {
    using namespace std;
    char answer;
    cout << "sizeof (Car)" << sizeof (Car) << endl;
    cout << "Shall I leak memory? [Y/N] ";
    answer = cin.get();
    leakMemory = answer == 'y' || answer == 'Y';
    functionA();
    }
